/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using Utils = System.Qizx.Util.Basic.Utils;
namespace System.Qizx.Util
{
	
	/// <summary> Generic string pattern used for string matching.</summary>
	public abstract class StringPattern
	{
		/// <summary> Value returned by method {@link #match(char[])}: perfect match.</summary>
		public const int MATCH = 0;
		/// <summary> Value returned by method {@link #match(char[])}: no match.</summary>
		public const int NOMATCH = 1;
		/// <summary> Value returned by method {@link #match(char[])}: no match and instance 
		/// is greater than pattern: used to quickly stop index iterator.
		/// </summary>
		public const int BEYOND = 2;
		
		
		protected internal char[] _pattern;
		protected internal char[] _fixedPrefix;
		protected internal int _fixedPrefixStart; // can be > length(fixedPrefix) (b/c escapes)
		protected internal bool _caseSensitive;
		
		public StringPattern()
		{
		}
		
		public StringPattern(char[] pattern, int length)
		{
			setPattern(pattern, length);
		}
		
		/// <summary> Redefinable matching method: returns match or not match.</summary>
		public virtual bool matches(char[] text)
		{
			return match(text) == MATCH;
		}
		
		public virtual bool matches(string value_Renamed)
		{
			return matches(value_Renamed.ToCharArray());
		}
		
		/// <summary> Redefinable matching method: returns values MATCH, NOMATCH or BEYOND.</summary>
		public abstract int match(char[] string_Renamed);
		
		/// <summary> Returns true if the pattern can only match one in a set of distinct values.
		/// (In contrast with regexp patterns for example which can match several
		/// different values).
		/// </summary>
		public virtual bool singleMatch()
		{
			return false;
		}
		
		
		protected internal virtual void  setPattern(char[] pattern, int length)
		{
			this._pattern = pattern;
			if (pattern != null)
			{
				this._pattern = Utils.subArray(pattern, 0, length);
				_fixedPrefix = this._pattern;
				_fixedPrefixStart = _fixedPrefix.Length;
			}
		}
		
		/// <summary> Returns the leading constant part of the pattern. Used to boost matching
		/// and lookup in indexes.
		/// Must not return null: return empty string instead.
		/// </summary>
		public virtual char[] fixedPrefix()
		{
			return _fixedPrefix;
		}
		
		public virtual char[] getPattern()
		{
			return _pattern;
		}
		
		public override string ToString()
		{
			return Utils.shortClassName(GetType()) + "(" + new System.String(_pattern) + ")";
		}
	}
}